<template>
	<view class="pay">
		<view class="money_panel">
			<view class="title"><text>应付金额</text></view>
			<view class="money">
				<text v-if="buy_type == 2">￥{{ price / stages }}元</text>
				<text v-else>￥{{ price }}元</text>
			</view>
			<view class="bottom">
				<view :class="buy_type == 1 ? 'list active' : 'list'" @tap="changeByType(1)">支付定金</view>
				<view :class="buy_type == 2 ? 'list active' : 'list'" @tap="changeByType(2)">分期付款</view>
				<view :class="buy_type == 3 ? 'list active' : 'list'" @tap="changeByType(3)">以租代购</view>
				<view :class="buy_type == 4 ? 'list active' : 'list'" @tap="changeByType(4)">全款支付</view>
			</view>
		</view>
		<view class="fenqi" v-if="buy_type == 2">
			<view class="qishu" @tap="changeStages(1)">
				<radio color="#999999" :checked="stages == 1" />
				<text>分期一年</text>
			</view>
			<view class="qishu" @tap="changeStages(2)">
				<radio color="#999999" :checked="stages == 2" />
				<text>分期两年</text>
			</view>
			<view class="qishu" @tap="changeStages(5)">
				<radio color="#999999" :checked="stages == 5" />
				<text>分期五年</text>
			</view>
			<view class="qishu" @tap="changeStages(10)">
				<radio color="#999999" :checked="stages == 10" />
				<text>分期十年</text>
			</view>
			<view class="qishu" @tap="changeStages(15)">
				<radio color="#999999" :checked="stages == 15" />
				<text>分期十五年</text>
			</view>
			<view class="qishu" @tap="changeStages(20)">
				<radio color="#999999" :checked="stages == 20" />
				<text>分期二十年</text>
			</view>
		</view>
		<view class="pannel">
			<view class="title"><text>|&nbsp;支付方式</text></view>
			<view class="list" @tap="changePayType(2)">
				<view class="left">
					<image src="../../../static/alipay.png" mode="widthFix"></image>
					<text>支付宝支付</text>
				</view>
				<view class="right"><image :src="`../../../static/${pay_type == 2 ? 'yuan_s' : 'yuan'}.png`" mode="widthFix"></image></view>
			</view>
			<view class="list" @tap="changePayType(1)">
				<view class="left">
					<image src="../../../static/wechat.png" mode="widthFix"></image>
					<text>微信支付</text>
				</view>
				<view class="right"><image :src="`../../../static/${pay_type == 1 ? 'yuan_s' : 'yuan'}.png`" mode="widthFix"></image></view>
			</view>
			<view class="list" @tap="changePayType(3)">
				<view class="left">
					<image src="../../../static/jinbi.png" mode="widthFix"></image>
					<text>金币支付</text>
				</view>
				<view class="right"><image :src="`../../../static/${pay_type == 3 ? 'yuan_s' : 'yuan'}.png`" mode="widthFix"></image></view>
			</view>
		</view>
		<view class="chengnuo">
			<view class="title">
				<image src="../../../static/selected.png" mode="widthFix"></image>
				<text>服务承诺</text>
			</view>
			<view class="content">
				免责条款：不可抗拒的自然灾害损坏，或人为操作损坏或严重超标运行产品而损坏（技术要求和使用环境），或故意损坏，修改产品标识等均不享受以上保修服务承诺。上班时间：上午九点——下午五点半。
			</view>
		</view>
		<view class="btn" @tap="pay()">立即付款</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			type: null,
			id: null,
			floor_id: null,
			price: 0.0,
			buy_type: 1,
			pay_type: 1,
			stages: 1,
			fromcart: false
		};
	},
	onLoad(option) {
		if (option.from_car == 1) {
			this.fromcart = true;
		} else {
			this.id = option.id;
			this.type = option.type;
			if (this.type == 1) {
				this.floor_id = option.floor_id;
			}
		}
		this.getData();
	},
	methods: {
		getData() {
			let authData = uni.getStorageSync('authData');
			if (authData.auth) {
				let data = {};
				if (this.fromcart) {
					data.fromcart = 1;
				} else {
					data = {
						type: this.type,
						goods_id: this.id
					};
				}
				if (this.type == 1) {
					data.floor_id = this.floor_id;
				}
				this.$request
					.post('/landed/createOrder', {
						data: data,
						header: {
							Authorization: authData.auth
						}
					})
					.then(res => {
						this.price = res.data.data.list.totalprice;
					});
			} else {
				uni.showToast({
					title: '请先登录',
					icon: 'none',
					duration: 2000
				});
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 2000);
			}
		},
		changeByType(type) {
			this.buy_type = type;
		},
		changePayType(type) {
			this.pay_type = type;
		},
		changeStages(stages) {
			this.stages = stages;
		},
		pay() {
			let authData = uni.getStorageSync('authData');
			if (authData.auth) {
				let data = {
					paytype: this.pay_type,
					buy_type: this.buy_type,
					stages: this.stages
				};
				if (this.fromcart) {
					data.fromcart = 1;
				} else {
					data.type = this.type;
					data.goods_id = this.id;
				}
				if (this.type == 1) {
					data.floor_id = this.floor_id;
				}
				this.$request
					.post('/landed/submitOrder', {
						data: data,
						header: {
							Authorization: authData.auth
						}
					})
					.then(res => {
						let orderinfo = res.data.data.list.orderinfo;
						let paymemt = res.data.data.list.paymemt;
						if (this.pay_type == 1) {
							console.log({
								provider: 'wxpay',
								orderInfo: orderinfo.order_sn,
								timeStamp: paymemt.timestamp,
								nonceStr: paymemt.noncestr,
								package: paymemt.prepayid,
								signType: 'MD5',
								paySign: paymemt.sign
							});
							uni.requestPayment({
								provider: 'wxpay',
								orderInfo: orderinfo.order_sn,
								timeStamp: paymemt.timestamp,
								nonceStr: paymemt.noncestr,
								package: paymemt.prepayid,
								signType: 'MD5',
								paySign: paymemt.sign,
								success: res => {
									uni.showToast({
										title: '支付成功',
										icon: 'none',
										duration: 2000
									});
								},
								fail: err => {
									uni.showToast({
										title: '支付失败',
										icon: 'none',
										duration: 2000
									});
								}
							});
						}
					});
			} else {
				uni.showToast({
					title: '请先登录',
					icon: 'none',
					duration: 2000
				});
				setTimeout(function() {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}, 2000);
			}
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f3f3f3;
}
.pay {
	width: 100%;
	padding: 20upx;
	box-sizing: border-box;
	background-color: #f3f3f3;
	.money_panel {
		width: 100%;
		height: 300upx;
		background: linear-gradient(left, #fd593f, #fc463e);
		border-radius: 20upx;
		box-sizing: border-box;
		padding: 20upx;
		padding-top: 50upx;
		color: #fff;
		overflow: hidden;
		.title {
			width: 100%;
			line-height: 1.5em;
			text-align: center;
			font-size: $uni-font-size-base;
		}
		.money {
			width: 100%;
			line-height: 1.8em;
			text-align: center;
			font-size: 50upx;
		}
		.bottom {
			width: 100%;
			height: 100upx;
			box-sizing: border-box;
			padding: 0 20upx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: $uni-font-size-base;
			.list {
				width: 25%;
				height: 70upx;
				line-height: 70upx;
				border-radius: 40upx;
				text-align: center;
				&.active {
					background-color: #fff;
					color: #fd593f;
				}
			}
		}
	}
	.pannel {
		width: 100%;
		background-color: #ffffff;
		margin-top: 20upx;
		border-radius: 20upx;
		box-sizing: border-box;
		padding: 20upx;
		.title {
			width: 100%;
			color: #f34b32;
			line-height: 1.5em;
			font-size: $uni-font-size-lg;
		}
		.list {
			width: 100%;
			height: 100upx;
			display: flex;
			line-height: 100upx;
			border-bottom: 2upx solid #e8e8e8;
			align-items: center;
			justify-content: space-between;
			font-size: $uni-font-size-base;
			color: #333;
			.left {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				image {
					width: 50upx;
				}
				text {
					margin-left: 10px;
				}
			}
			.right {
				display: flex;
				align-items: center;
				justify-content: flex-start;
				image {
					width: 50upx;
				}
			}
		}
	}
	.chengnuo {
		width: 100%;
		.title {
			width: 100%;
			color: #f34b32;
			height: 100upx;
			font-size: $uni-font-size-lg;
			display: flex;
			align-items: center;
			justify-content: flex-start;
			image {
				width: 50upx;
			}
			text {
				color: #333;
				margin-left: 15upx;
			}
		}
		.content {
			font-size: $uni-font-size-base;
			color: #333;
		}
	}
	.btn {
		margin-top: 40upx;
		width: 100%;
		height: 100upx;
		line-height: 100upx;
		border-radius: 50upx;
		text-align: center;
		font-size: $uni-font-size-lg;
		color: #fff;
		background: linear-gradient(left, #ff5d42, #f33632);
	}
	.fenqi {
		width: 100%;
		color: #999999;
		padding: 20upx 0;
		font-size: $uni-font-size-base;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
		.qishu {
			width: 33%;
			margin-top: 20upx;
		}
	}
}
</style>
